import EventCenter from "../../TFrame/script/helper/EventCenter";
import EventNames from "../EventNames";
import FireBaseAPI from "../FireBaseAPI";
import StatusProperties, { RewardType } from "../game/StatusProperties";
import NativeCaller from "../NativeCaller";
import { ActionType } from "../net/Enum";
import ServiceLogic from "../net/ServerLogic";
import CoinSystem from "../system/CoinSystem";
import Util from "../../../StartAb/Util";
import WXAD from "../../../StartAb/WXAd";

const { ccclass, property } = cc._decorator;
/**
 * 1. 领取成功后，需要等待指定时间(30秒)
 * 2. 启动时检查时间到没有
 */
@ccclass
export default class GetCoinFreeDialog extends cc.Component {

    //防止多开
    static isShowing = false;
    @property(cc.Label)
    timeLabel: cc.Label = null;

    @property(cc.Button)
    getFreeButton: cc.Button = null;

    FREE_COIN = 100

    static next_ok_ts = 0;
    static waiting_ms = 30000;

    static show(parent) {
        if(this.isShowing == true)
            return
        this.isShowing = true;
        cc.assetManager.getBundle("GameAb").load("prefab/GetCoinFreeRoot", cc.Prefab, (err: Error, pf: cc.Prefab) => {
            if (err) {
                this.isShowing = false;
                return console.log(err.message);
            }
            if (parent && parent.isValid) {
                let dialog = cc.instantiate(pf);
                dialog.parent = parent;
            }
        })
    }

    onDestroy() {
        GetCoinFreeDialog.isShowing = false;
    }

    protected onEnable(): void {
        WXAD.instance.showBanner();
    }

    protected onDisable(): void {
        WXAD.instance.hideBanner();
    }


    start() {
        let designedResolution = cc.Canvas.instance.designResolution;
        this.node.scale = designedResolution.width / 640
        if (GetCoinFreeDialog.next_ok_ts > Date.now()) {
            this.getFreeButton.interactable = false;
            this.timeLabel.node.active = true;
            this.schedule(this.update0Point2, 0.2);
        }
        EventCenter.on(EventNames.RewardedSuccess, () => {
            EventCenter.emit(EventNames.FlyGetCoinEvent, this.FREE_COIN, this.node.convertToWorldSpaceAR(cc.Vec2.ZERO));
            CoinSystem.addCoin(this.FREE_COIN);
            GetCoinFreeDialog.next_ok_ts = Date.now() + GetCoinFreeDialog.waiting_ms   //1分钟
            // FireBaseAPI.logEvent("Event_GetFreeCoin", {"coinhave":CoinSystem.coinInMomory})
            this.timeLabel.node.active = true;
            this.getFreeButton.interactable = false;
            this.updateTimerString();
            this.unschedule(this.update0Point2);
            this.schedule(this.update0Point2, 0.2);
            ServiceLogic.action(ActionType.ActionAdToCoin, {});
            ServiceLogic.action(ActionType.ActionCoinChanged, { "coinhave": CoinSystem.coinInMomory, "beiz": this.FREE_COIN })
        }, this)
    }

    getClick() {
        StatusProperties.rewardType = RewardType.ReqGetFreeCoin;
        NativeCaller.showReward();
    }

    updateTimerString() {
        let sec = (GetCoinFreeDialog.next_ok_ts - Date.now()) / 1000;
        let str = Util.secondToStr(sec, false);
        this.timeLabel.string = str;
    }

    update0Point2() {
        if (GetCoinFreeDialog.next_ok_ts <= Date.now()) {
            this.unschedule(this.update0Point2);
            this.getFreeButton.interactable = true;
            this.timeLabel.node.active = false;
            return;
        }
        this.updateTimerString();
    }
}
